home *** CD-ROM | disk | FTP | other *** search
- /* TTextView.cpp
- *
- * This contains my code to handle text views
- */
-
- #include "MyApp.h"
- #include <XStdScrollView.h>
-
- /************************************************************************/
- /* */
- /* Construction/Destruction */
- /* */
- /************************************************************************/
-
- /* TTextView::TTextView
- *
- * Construct this thing
- */
-
- TTextView::TTextView(XGView *parent, XGArgStream &s) : XGView(parent,s)
- {
- fFile = NULL;
- }
-
- /* TTextView::~TTextView
- *
- * Destruct this thing
- */
-
- TTextView::~TTextView()
- {
- if (fFile) fclose(fFile);
- }
-
- /************************************************************************/
- /* */
- /* Control */
- /* */
- /************************************************************************/
-
- /* TTextView::ShowFile
- *
- * Show this thing
- */
-
- void TTextView::ShowFile(FILE *f)
- {
- char buffer[256];
-
- fFile = f;
- if (fFile == NULL) return;
-
- fseek(f,0,SEEK_SET);
- fStart += ftell(f);
- while (fgets(buffer,255,f)) fStart += ftell(f);
-
- RecalcScroll();
- InvalView();
- }
-
- /* TTextView::DoSizeView
- *
- * Resize
- */
-
- void TTextView::DoSizeView()
- {
- XGView::DoSizeView();
- RecalcScroll();
- }
-
- /* TTextView::DoDrawView
- *
- * Draw me
- */
-
- void TTextView::DoDrawView(Rect r)
- {
- XGScrollView *v;
- char buffer[256];
- long s,e,l;
- short x,y;
- char *ptr;
- Rect t;
-
- // initialize
- v = dynamic_cast<XGScrollView *>(GetParent());
- if (v == NULL) return;
- XGDraw draw(this);
-
- // start scrolling range
- v->GetScrollValue(&x,&y);
- s = y + r.top / 12;
- e = y + (r.bottom + 11) / 12;
- if (e >= fStart.Length()) e = fStart.Length() - 1;
-
- // draw the lines of text
- t = r;
- for (l = s; l < e; l++) {
- fseek(fFile,fStart[l],SEEK_SET);
- fgets(buffer,255,fFile);
- for (ptr = buffer; (*ptr != 0) && (*ptr != '\n'); ptr++) ;
- *ptr = 0;
-
- t.top = (l-y)*12;
- t.bottom = t.top + 12;
-
- #if OPT_MACOS == 1
- ::EraseRect(&t);
- ::MoveTo(5,(l-y)*12+10);
- ::DrawString(c2pstr(buffer));
- #endif
-
- #if OPT_WINOS == 1
- ::FillRect(draw.GetDC(),&t,GetStockObject(WHITE_BRUSH));
- ::TextOut(draw.GetDC(),5,(l-y)*12,buffer,strlen(buffer));
- #endif
- }
-
- if (t.bottom < r.bottom) {
- r.top = t.bottom;
- #if OPT_MACOS == 1
- ::EraseRect(&r);
- #endif
-
- #if OPT_WINOS == 1
- ::FillRect(draw.GetDC(),&r,GetStockObject(WHITE_BRUSH));
- #endif
- }
- }
-
- /************************************************************************/
- /* */
- /* Scroll management */
- /* */
- /************************************************************************/
-
- /* TTextView::RecalcScroll
- *
- * Recalculate the scrollbar
- */
-
- void TTextView::RecalcScroll()
- {
- XGScrollView *v;
- Rect h;
- short page;
-
- v = dynamic_cast<XGScrollView *>(GetParent());
- if (v == NULL) return;
-
- h = GetContentRect();
- page = (h.bottom - h.top) / 12;
- if (page < 1) page = 1;
-
- v->SetScrollPage(0,page);
- page = fStart.Length() - page;
- if (page < 0) page = 0;
- v->SetScrollMax(0,page);
- }
-